package cn.lili.modules.payment.serviceimpl;


import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.payment.entity.dos.PaymentLog;
import cn.lili.modules.payment.entity.dto.PaymentLogSearchParams;
import cn.lili.modules.payment.entity.dto.ProfitSharingRequestDTO;
import cn.lili.modules.payment.entity.dto.UserPointUpdateDTO;
import cn.lili.modules.payment.entity.enums.PaymentClientEnum;
import cn.lili.modules.payment.entity.enums.PaymentMethodEnum;
import cn.lili.modules.payment.mapper.PaymentLogMapper;
import cn.lili.modules.payment.service.PaymentLogService;
import cn.lili.modules.wallet.manage.WalletMange;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;


/**
 * 支付唤起业务实现
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/3/28 14:33
 */

@Service
@Slf4j
public class PaymentLogServiceImpl extends ServiceImpl<PaymentLogMapper, PaymentLog> implements PaymentLogService {

    @Resource
    private WalletMange walletMange;

    @Override
    public PaymentLog getPaymentlog(String payOrderNo) {
        return this.getOne(new LambdaQueryWrapper<PaymentLog>()
                .eq(PaymentLog::getOutTradeNo, payOrderNo));
    }


    @Override
    public Boolean getPaymentResult(String sn) {
        LambdaQueryWrapper<PaymentLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentLog::getOrderSn, sn);
        queryWrapper.eq(PaymentLog::getIsPay, true);
        boolean payCount = this.count(queryWrapper) > 0;
        log.info("payCount === {}", payCount);
        return payCount;
    }

    @Override
    public Page<PaymentLog> queryPaymentLogs(PaymentLogSearchParams searchParams) {
        /*LambdaQueryWrapper<PaymentLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentLog::getIsPay, true);
        queryWrapper.orderByDesc(PaymentLog::getCreateTime);*/

        LambdaQueryWrapper<PaymentLog> queryWrapper = searchParams.generateWrapper();
        queryWrapper.orderByDesc(PaymentLog::getCreateTime);
        return this.page(PageUtil.initPage(searchParams), queryWrapper);
    }

    @Override
    public List<PaymentLog> getPaymentLogByCombineOutTradeNo(String combineOutTradeNo) {
        LambdaQueryWrapper<PaymentLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentLog::getCombineOutTradeNo, combineOutTradeNo);
        return this.list(queryWrapper);
    }

    @Override
    public PaymentLog getPaymentLogByOutTradeNo(String outTradeNo) {
        LambdaQueryWrapper<PaymentLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentLog::getOutTradeNo, outTradeNo);
        return this.getOne(queryWrapper);
    }

    @Override
    public PaymentLog getPaymentLogByOrderSN(String orderSn) {
        LambdaQueryWrapper<PaymentLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentLog::getOrderSn, orderSn);
        queryWrapper.eq(PaymentLog::getIsPay, true);
        queryWrapper.eq(PaymentLog::getIsRepeat, false);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper, true);
    }

    @Override
    public PaymentLog generateZeroLog(ProfitSharingRequestDTO profitSharingRequestDTO) {
        PaymentLog paymentLog =
                PaymentLog.builder()
                        .paymentMethod(PaymentMethodEnum.BANK_TRANSFER.name())
                        .paymentClient(PaymentClientEnum.NATIVE.name())
                        .orderSn(profitSharingRequestDTO.getOrderSn())
                        .price(0D)
                        .payerId("")
                        .payeeId("")
                        .mchId("")
                        .isCheck(true)
                        .isPay(true)
                        .isRepeat(false)
                        .isRefund(false)
                        .outTradeNo(SnowFlake.createStr("OT"))
                        .transactionId(SnowFlake.createStr("T"))
                        .paymentScene(profitSharingRequestDTO.getPaymentSceneEnums().name())
                        .description("0元订单支付")
                        .isCombine(false)
                        .build();
        this.save(paymentLog);
        return paymentLog;
    }


    @Override
    public ResultMessage<Object> memberCastellan(UserPointUpdateDTO dto) {
        walletMange.memberCastellan(dto);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage<Object> memberUnion(UserPointUpdateDTO dto) {
        walletMange.memberUnion(dto);
        return ResultUtil.success();
    }
}